﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using iStudio.Structure.Wave;

namespace iStudio.Structure.Project
{
    public class WaveProject : INotifyPropertyChanged
    {
        private readonly ObservableCollection<WaveTrack> _trackList;
        private readonly ObservableCollection<WaveChunk> _selectedChunks; 

        public string Name
        {
            get;
            set;
        }

        public double Length
        {
            get;
            set;
        }

        public WaveFormat Format
        {
            get;
            set;
        }

        public WaveProject ()
        {
            Format = new WaveFormat
                         {
                             SampleRate = 44100,
                             BitsPerSample = 32,
                             Channels = 2
                         };

            _trackList = new ObservableCollection<WaveTrack>();
            _selectedChunks = new ObservableCollection<WaveChunk>();
        }

        public WaveProject (WaveFormat format)
        {
            Format = format;

            _trackList = new ObservableCollection<WaveTrack>();
            _selectedChunks = new ObservableCollection<WaveChunk> ();
        }

        public ObservableCollection<WaveTrack> TrackList
        {
            get
            {
                return _trackList;
            }
        }

        public ObservableCollection<WaveChunk> SelectedChunks
        {
            get
            {
                return _selectedChunks;
            }
        }

        public void DeselectAll()
        {
            foreach (WaveTrack track in TrackList)
            {
                foreach (WaveChunk chunk in track.TrackChunks)
                {
                    chunk.IsSelected = false;
                }
            }
        }

        public void AddTrack(WaveTrack waveTrack)
        {
            if (waveTrack.Length > (Length * Format.SampleRate))
            {
                Length = waveTrack.Length / (double)Format.SampleRate ;
            }

            TrackList.Add(waveTrack);
        }

        public void AddTrack (List<WaveTrack> waveTracks)
        {
            foreach (WaveTrack track in waveTracks)
            {
                AddTrack (track);
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
